<?php
namespace cms\modules\v1\models\feedback;

use cms\modules\common\models\Admin;
use common\models\Functions;
use common\models\Status;
use common\models\user\User;
use yii\base\Exception;
use yii\helpers\ArrayHelper;
use Yii;
/**
 * This is the model class for table "goods".
 *
 * @property int $id
 * @property string $name
 */
class FeedbackInfo extends \common\models\feedback\FeedbackInfo
{
    /**
     * 返回列表数据
     * @param int $page
     * @param int $size
     * @return array
     */
    public function getListData($page = 1, $size = 20) {
        try {

            //搜索条件
            $searchWhere  = $this->getSearch();//自定义搜索条件

            //得到文章的总数
            $count = self::getCountByAndWhere($searchWhere['where'], $searchWhere['andWhere']);
            $data = static::setPageinationAPI($page, $size, $count);

            //设置分页
            $offset = ($data['page'] - 1) * $size;

            //排序 order
            $orderSql = 'id DESC';

            $query = static::getObjectByAndWhere($searchWhere['where'],$searchWhere['andWhere'], '*', $orderSql, $offset, $size);

            $data['data'] = $this->handelList($query);

            return Functions::formatRestAPI(0, '', $data);
        } catch (Exception $e) {
            return Functions::formatRestAPI(1, $e->getMessage());
        }
    }

    /**
     * 自定义参数的搜索  搜索,搜索也分每一列
     * 这里要根据业务逻辑进行修改
     * @return string
     */
    public function getSearch () {
        $where = $andWhere = [];
        $search = Yii::$app->request->get();
        $where['is_delete'] = Status::COMMON_IS_NOT_DELETE;
        if(!empty($search)){
            $status = isset($search['status']) ? $search['status'] : 0;
//            $type = isset($search['type']) ? $search['type'] : '';
            $phone = isset($search['phone']) ? $search['phone'] : '';
//            $range = isset($search['range']) ? $search['range'] : '';

            if($status) {
                $where['status'] = (int) $status;
            }

//            if($type) {
//                $messageTextWhere['feedback_type'] = (int) $type;
//            }

            if ($phone) {
                $where['user_id'] = User::getFormArrayAndWhere([],['like','phone',$phone],'id','id');
            }

//            if ($range) {
//                $andWhere[] = ["between", "create_time", $range[0], $range[1]];
//            }

        }
        return [
            'where' => $where,
            'andWhere' => $andWhere
        ];
    }

    /**
     * 进行初始化数据处理
     * @param array $query
     * @return array
     */
    public function handelList($query = []) {
        if (empty($query)) {
            return null;
        }

        $list = $query->with([
            'user' => function($q) {
                $q->select(['id','login_name','nick_name','phone','user_face']);
            },

//            'type' => function($q) {
//                $q->select(['id','name']);
//            },

            'dealAdmin' => function($q) {
                $q->select(['id','nick_name']);
            },
        ])->asArray()->all();

        foreach ($list as $k => &$v) {
            $singleUserInfo = ArrayHelper::getValue($v,'user',[]);
            $singleUserInfo['user_face'] = $singleUserInfo ? static::getAvatar($singleUserInfo['user_face']) : '';
//            $singleTypeInfo = ArrayHelper::getValue($v,'type',[]);
            $singleAdminInfo = ArrayHelper::getValue($v,'dealAdmin',[]);


            $v['user_face'] = $singleUserInfo['user_face'];
            $v['user_phone'] = $singleUserInfo['phone'];
            $v['user_nick_name'] = $singleUserInfo['nick_name'];
//            $v['type'] = $singleTypeInfo ? $singleTypeInfo['name'] : '';
            $v['time'] = $v['create_time'];
            $v['status_name'] = $v['status'] == 1 ? "待处理" : "已处理";
            $v['deal_admin'] = $singleAdminInfo ? $singleAdminInfo['nick_name'] : '';
            $v['images'] = static::getAllImages($v['images']);
        }
        return $list;
    }

    public function getInfo($id = 0) {
        try {
            $info = static::getOneInfoById($id);
            
            if (empty($info)) {
                throw new Exception('未知反馈信息');
            }
//            $type = FeedbackType::getOneInfoById($info['feedback_type']);
            $data['id'] = $info['id'];
            $data['summary'] = $info['summary'];
//            $data['feedback_type'] = $type['name'];
            $data['reply'] = $info['replay'] ?? '';
            $data['phone'] = $info['phone'];
            $data['create_time'] = $info['create_time'];

            return Functions::formatRestAPI(0, '', $data);
        } catch (Exception $e) {
            return Functions::formatRestAPI(1, $e->getMessage());
        }
    }
    
    public function saveInfo() {
        try {
            $post = \Yii::$app->request->post();
            
            $admin = (new Admin())->checkLogin();
            $replay = $post['reply'];
            if (empty($replay)) {
                throw new Exception("处理内容不能为空");
            }
            
            $status = $post['status'];
            if (empty($status)) {
                throw new Exception("处理状态不能为空");
            }
            
            $insert['reply'] = $replay;
            $insert['reply_time'] = date("Y-m-d H:i:s");
            $insert['status'] = $status;
            
            $insert['deal_admin'] = $admin['id'];
            
            
            $id = (int) $post['id'];
            if (empty($id)) {
                throw new Exception("未知信息");
            }
    
            static::updateData($insert, ['id' => $id]);
            
            return Functions::formatRestAPI(0, '操作成功');
        } catch (Exception $e) {
            return Functions::formatRestAPI(1, $e->getMessage());
        }
    }

    public function multipleReply() {
        try {
            $admin = (new Admin())->checkLogin();
            
            $post = \Yii::$app->request->post();
            
            $ids = is_array($post['id']) ? $post['id'] : [$post['id']];
            if (empty($ids)) {
                throw new Exception("未选择信息");
            }
            
            if (empty($post['reply'])) {
                throw new Exception("处理信息不能为空");
            }
            
            $data['reply'] = $post['reply'];
            $data['reply_time'] = date("Y-m-d H:i:s");
            $data['status'] = Status::COMMON_RADIO_NO;
    
            $data['deal_admin'] = $admin['id'];

            static::updateData($data, ['id' => $ids, "status" => Status::COMMON_RADIO_YES]);
            return Functions::formatRestAPI(0, '回复成功');
        } catch (Exception $e) {
            return Functions::formatRestAPI(1, $e->getMessage());
        }
    }
    
    
    public function multipleDelete() {
        try {
            $admin = (new Admin())->checkLogin();
            
            $post = \Yii::$app->request->post();
            
            $ids = array_column($post['id'], "id");
            if (empty($ids)) {
                throw new Exception("未选择信息");
            }
            
            if (empty($post['reply'])) {
                throw new Exception("回复信息不能为空");
            }
            
            $data['is_delete'] = Status::COMMON_RADIO_YES;
            $data['deal_admin'] = $admin['id'];
            
            static::updateData($data, ['id' => $ids, "is_delete" => Status::COMMON_RADIO_NO]);
            return Functions::formatRestAPI(0, '');
        } catch (Exception $e) {
            return Functions::formatRestAPI(1, $e->getMessage());
        }
    }

    public function queryAudit() {
        $where['status'] = Status::COMMON_RADIO_YES;
        $where['is_delete'] = Status::COMMON_IS_NOT_DELETE;

        $count = static::getCountByWhere($where);

        return Functions::formatRestAPI(0, '', $count);
    }
}
